const moment = require('moment')
const path = require('path')
const db = require('../db/index')
exports.addArticle = (req, res) => {
    if (!req.file || req.file.fieldname !== 'cover_img') return res.cc('请先上传文件')

    const sql = `insert into ev_articles set ?`

    const art = {
        ...req.body,
        pub_date: moment().format('YYYY-MM-DD HH:mm:ss'),
        author_id: req.user.id,
        cover_img: path.join('uploads', req.file.filename)
    }
    console.log(art);
    db.query(sql, [art], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('文章发布失败')
        res.send({
            status: 0,
            message: '文章发布成功'
        })
    })
}